<template>
  <ContentWrap>
    <div :class="searchVisible ? 'flex flex-row flex-wrap mb-2' : 'flex flex-row flex-wrap'">
      <el-button v-if="showBack" @click="router.back()">
        <Icon icon="ep:arrow-left" class="mr-5px" />返回
      </el-button>
      <el-button plain type="primary" @click="openForm('create')" v-hasRole="['shop_admin']">
        <Icon class="mr-5px" icon="ep:plus" />
        新增
      </el-button>
      <el-button plain type="success" @click="searchVisible = !searchVisible">
        <Icon class="mr-5px" icon="ep:operation" />
        {{ searchVisible ? '关闭筛选' : '打开筛选' }}
      </el-button>
      <el-text type="info">
        <span class="ml-5px">{{ searchVisible ? '关闭以下筛选条件' : '点击打开筛选条件' }}</span>
      </el-text>
      <div class="flex-1"></div>
      <el-button @click="resetQuery">
        <Icon class="mr-5px" icon="ep:refresh" />
        刷新列表
      </el-button>
    </div>
    <el-form v-if="searchVisible" ref="searchFormRef" :model="tableData.searchParam" inline>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="tableData.searchParam.name"
          class="!w-240px"
          placeholder="请输入名称关键词"
          clearable
          @keyup="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="创建时间">
        <el-date-picker
          v-model="tableData.searchParam.createTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="mt-[5px]">
      <el-table :data="tableData.list" size="large" v-loading="tableData.loading">
        <template #empty>
          <span>{{ !tableData.loading ? '没有相关数据' : '' }}</span>
        </template>
        <el-table-column label="名称" min-width="100">
          <template #default="{ row }">
            <div>{{ row.name }}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="图片" min-width="100">
          <template #default="{ row }">
            <el-image
              :src="row.picUrl || '/placeholder.png'"
              :preview-src-list="[row.picUrl || '/placeholder.png']"
              :initial-index="0"
              :preview-teleported="true"
              :hide-on-click-modal="true"
              class="cursor-pointer"
            />
          </template>
        </el-table-column> -->
        <el-table-column label="简介" align="left" min-width="150">
          <template #default="{ row }">
            <div v-if="row.summary">{{ row.summary }}</div>
            <div>最多适用于 {{ row.people }} 人就餐</div>
          </template>
        </el-table-column>
        <el-table-column
          v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
          label="所属店铺"
          min-width="120"
        >
          <template #default="{ row }">
            <div class="flex flex-col items-start justify-start">
              <img
                class="w-[50px] h-[50px] mr-[10px] border border-solid border-gray-50"
                :src="row.shop?.logo || '/placeholder.png'"
                alt=""
              />
              <span>{{ row.shop?.name || '未知' }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="80" sortable>
          <template #default="{ row }">
            <div>{{ row.enable ? '启用' : '禁用' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="排序" min-width="60" sortable>
          <template #default="{ row }">
            <div>{{ row.sort }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="scope">
            <el-button
              v-hasRole="['super_admin', 'platform_admin', 'tenant_admin', 'shop_admin']"
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
            >
              编辑
            </el-button>
            <el-button
              v-hasRole="['shop_admin']"
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        v-model:limit="tableData.searchParam.pageSize"
        v-model:page="tableData.searchParam.pageNo"
        :total="tableData.total"
        @pagination="loadTableData"
      />
    </div>
  </ContentWrap>

  <DeskCategoryForm ref="formRef" @success="loadTableData" />
</template>

<script lang="ts" setup name="DeskCategoryList">
import * as DeskCategoryApi from '@/api/dine/desk/DeskCategoryApi'
import { FormInstance } from 'element-plus'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { fenToYuan } from '@/utils'
import DeskCategoryForm from './DeskCategoryForm.vue'

const { t } = useI18n()
const message = useMessage()
const router = useRouter()

const searchVisible = ref(false)

const tableData = reactive({
  total: 0,
  loading: true,
  list: [],
  searchParam: {
    pageNo: 1,
    pageSize: 15,
    name: undefined,
    createTime: []
  }
})

const loadTableData = () => {
  tableData.loading = true
  DeskCategoryApi.getPage({
    ...tableData.searchParam
  })
    .then((res: any) => {
      tableData.loading = false
      tableData.list = res.list
      tableData.total = res.total
    })
    .catch(() => {
      tableData.loading = false
    })
}

const searchFormRef = ref<FormInstance>()

const handleQuery = () => {
  tableData.searchParam.pageNo = 1
  loadTableData()
}

const resetQuery = () => {
  searchFormRef.value?.resetFields()
  tableData.searchParam = {
    pageNo: 1,
    pageSize: 15,
    name: undefined,
    createTime: []
  }
  handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number | string) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number | string) => {
  try {
    await message.delConfirm()
    await DeskCategoryApi.deleteById(id)
    message.success(t('common.delSuccess'))
    loadTableData()
  } catch {}
}

const showBack = ref(false)
onMounted(() => {
  handleQuery()
})
</script>
